[#ftl]
import { Injectable } from '@angular/core';
import { ApolloQueryResult, FetchResult } from '@apollo/client/core';
import {Apollo, gql, MutationResult, QueryRef} from 'apollo-angular';
import { Observable } from 'rxjs';
import { ${serviceInfo.name}Api,
[#list serviceInfo.queries.keySet() as qkey][#assign query=serviceInfo.queries[qkey]]
${serviceInfo.name}_${query.name}Result,
[#if query.params?size gt 0]
${serviceInfo.name}_${query.name}Variables,
[/#if][/#list]
    [#list serviceInfo.mutations.keySet() as qkey][#assign mutation=serviceInfo.mutations[qkey]]
${serviceInfo.name}_${mutation.name}Result,
[#if mutation.params?size gt 0]
${serviceInfo.name}_${mutation.name}Variables,
[/#if]
    [/#list]
${serviceInfo.modelName}ResultInfo, ${serviceInfo.modelName}PageInfo,
${serviceInfo.modelName}Connection,
${serviceInfo.modelName}Package,
${serviceInfo.modelName}OrderBy,
${serviceInfo.modelName}MutationInfo,
${serviceInfo.modelName}StringFilter,
${serviceInfo.modelName}NumberFilter,
${serviceInfo.modelName}BooleanFilter,
${serviceInfo.beanName}Query,
${serviceInfo.beanName}Mutation,
${serviceInfo.name}QueryOptions,
[#list (serviceInfo.types.keySet()) as key]
    [#assign type=serviceInfo.types[key]]
${type.name},
[/#list]
${serviceInfo.modelName}ErrorBody} from '../model/${fileName}.model';

/************************************************************************
 * ${serviceInfo.comment}
 */
@Injectable({
    providedIn: 'root'
})
export class ${serviceInfo.name} implements ${serviceInfo.name}Api {

[#list serviceInfo.queries.keySet() as qkey][#assign query=serviceInfo.queries[qkey]]
        private ${query.name}Gql:string;
    [#if query.queryMode == "MORE"]
        private ${query.name}ObjectRef!: QueryRef<${serviceInfo.name}_${query.name}Result>;
    [/#if]
[/#list]

    constructor(private apollo: Apollo) {
    [#list serviceInfo.queries.keySet() as qkey][#assign query=serviceInfo.queries[qkey]]
        this.${query.name}Gql=`query ${root}_${serviceInfo.beanName}_${query.name}[#if query.params?size gt 0]([#list query.params as param][#if param_index!=0],[/#if]${gqlParamMap(param)}[/#list])[/#if]{${'$'}{${serviceInfo.beanName}Query.${serviceInfo.beanName}_${query.name}}}`;
    [/#list]
    }
    [#list serviceInfo.queries.keySet() as qkey][#assign query=serviceInfo.queries[qkey]]
    /**
     * ${query.comment}
     */
    ${query.name}([#if query.params?size gt 0]variables: ${serviceInfo.name}_${query.name}Variables, [/#if]options:${serviceInfo.name}QueryOptions = { cachePolicy: '${query.cachePolicy}', more: false }): Observable<ApolloQueryResult<${serviceInfo.name}_${query.name}Result>> {
    [#if query.queryMode == "MORE"]
        if(!this.${query.name}ObjectRef){
            this.${query.name}ObjectRef = this.apollo.watchQuery<${serviceInfo.name}_${query.name}Result>({ query: gql`${'$'}{this.${query.name}Gql}`, fetchPolicy:'${query.cachePolicy}' });
        }
    [#if query.params?size gt 0]
        this.${query.name}ObjectRef.setVariables(variables);
        if(options.more){
            this.${query.name}ObjectRef.fetchMore({variables});
        }
        [/#if]
        return this.${query.name}ObjectRef.valueChanges;
        [#elseif query.queryMode == "ONCE"]
            return this.apollo.query({ query: gql`${'$'}{this.${query.name}Gql}`[#if query.params?size gt 0],variables [/#if],fetchPolicy:options.cachePolicy});
            [#else]
            return {} as any;
    [/#if]
    }
    [/#list]
    [#list serviceInfo.mutations.keySet() as qkey][#assign mutation=serviceInfo.mutations[qkey]]
    /**
     * ${mutation.comment}
     */
    ${mutation.name}([#if mutation.params?size gt 0]variables: ${serviceInfo.name}_${mutation.name}Variables[/#if]): Observable<MutationResult<${serviceInfo.name}_${mutation.name}Result>>{
        return this.apollo.mutate({mutation: gql`mutation ${root}_${serviceInfo.beanName}_${mutation.name}[#if mutation.params?size gt 0]([#list mutation.params as param][#if param_index!=0],[/#if]${gqlParamMap(param)}[/#list])[/#if]{${'$'}{${serviceInfo.beanName}Mutation.${serviceInfo.beanName}_${mutation.name}}}`, variables,fetchPolicy:'no-cache'});
    }
    [/#list]









}
